/**
 * \ * Copyright &copy; 2012-2014 <a href="https://github.cn.apestar">JeeSite</a> All rights reserved.
 */
package cn.apestar.modules.sys.web;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.apestar.common.DataContants;
import cn.apestar.common.chat.ChatInstance;
import cn.apestar.common.chat.enums.MessageType;
import cn.apestar.common.chat.enums.MessageWTType;
import cn.apestar.common.config.Global;
import cn.apestar.common.exception.BaseException;
import cn.apestar.common.huanxin.PeoPleType;
import cn.apestar.common.image.Auth;
import cn.apestar.common.utils.CacheUtils;
import cn.apestar.common.utils.CommonUtil;
import cn.apestar.common.utils.StringUtils;
import cn.apestar.common.web.BaseController;
import cn.apestar.modules.common.school.init.BaiduMap;
import cn.apestar.modules.common.school.init.OrgInit;
import cn.apestar.modules.sys.entity.Area;
import cn.apestar.modules.sys.utils.DictUtils;
import cn.apestar.modules.sys.utils.UserUtils;

/**
 * 公共参数返回
 * @author LVH
 * @version 2015-07-11
 */
@Controller
@RequestMapping(value = "${adminPath}/c/")
public class CommonController extends BaseController {

    private Object removeAll;

    /**
     * 获取七牛云TOKEN
     */
    /**
     * 生成上传token
     * @param bucket 空间名
     * @param key key，可为 null
     * @param expires 有效时长，单位秒
     * @param policy 上传策略的其它参数，如 new StringMap().put("endUser", "uid").putNotEmpty("returnBody", "")。 scope通过 bucket、key间接设置，deadline 通过 expires 间接设置
     * @return 生成的上传token
     */
    @ResponseBody
    @RequestMapping(value = {"uploadToken"})
    public Object uploadToken(HttpServletRequest request) {

        String returnBody = request.getParameter("returnBody");
        Map map = new HashMap();
        map.put("token", Auth.create(Global.getConfig("qiniu_access_key"), Global.getConfig("qiniu_secret_key"),
                                     Global.getConfig("qiniu_bucket_name"), returnBody));
        map.put("domin", Global.getConfig("qiniu_domain"));
        return getSuccessMessageObject("success", map);
    }

    /**
     * 换取年度下拉框
     */
    @ResponseBody
    @RequestMapping(value = {"getBusinessYear"})
    public Object getBusinessYear() {

        List list = UserUtils.getBusinessYear();
        return getSuccessMessageObject("success", list);
    }

    /**
     * 获取区域
     */
    @ResponseBody
    @RequestMapping(value = {"getAreaList"})
    public Object getAreaList(HttpServletRequest request) {

        String key = "";
        Area area = new Area();
        // 级别
        String level = request.getParameter("level");
        if (!StringUtils.isEmpty(level)) {
            // level = "1";
            area.setType(level);
            key = key + level;
        }
        // 父节点
        String parentId = request.getParameter("parentId");
        if (!StringUtils.isEmpty(parentId)) {
            area.setParentId(parentId);
            key = key + "p" + parentId;
        }

        List list = UserUtils.getAreaList(area, key);
        return getSuccessMessageObject("", list);
    }

    /**
     * 获取城市
     */
    @ResponseBody
    @RequestMapping(value = {"getCity"})
    public Object getCity(HttpServletRequest request, String areaName, String parentId) {

        return getSuccessMessageObject("success", UserUtils.getAreaByLevel(DataContants.AREA_SHI, areaName, parentId));
    }

    /**
     * 获取省
     */
    @ResponseBody
    @RequestMapping(value = {"getSheng"})
    public Object getSheng(HttpServletRequest request, String areaName, String parentId) {

        return getSuccessMessageObject("success", UserUtils.getAreaByLevel(DataContants.AREA_SHENG, areaName, parentId));
    }

    /**
     * 获取区/县
     */
    @ResponseBody
    @RequestMapping(value = {"getCountry"})
    public Object getCountry(HttpServletRequest request, String areaName, String parentId) {

        return getSuccessMessageObject("success", UserUtils.getAreaByLevel(DataContants.AREA_XIAN, areaName, parentId));
    }

    /**
     * 获取性别
     */
    @ResponseBody
    @RequestMapping(value = {"getSex"})
    public Object getSp(HttpServletRequest request) {

        // 父节点
        List list = DictUtils.getDictListMap("MT_SEX");
        return getSuccessMessageObject("", list);
    }

    /**
     * 获取所有类型
     */
    @ResponseBody
    @RequestMapping(value = {"getAll"})
    public Object getAll() {

        Map map = new HashMap();

        // 性别
        map.put("MT_SEX", DictUtils.getDictListMap("MT_SEX"));
        // 国籍
        map.put("MT_MTGJ", UserUtils.getAreaByLevel(DataContants.AREA_GJ, null, null));
        // 城市
        map.put("MT_MTSH", UserUtils.getAreaByLevel(DataContants.AREA_SHENG, null, null));
        // 城市
        map.put("MT_MTCS", UserUtils.getAreaByLevel(DataContants.AREA_SHI, null, null));
        // 模特类别
        map.put("MT_MTLB", DictUtils.getDictListMap("MT_MTLB"));
        return getSuccessMessageObject("", map);
    }

    @ResponseBody
    @RequestMapping(value = {"removeCache"})
    public Object removeCache() {

        CacheUtils.removeAll(CacheUtils.SYS_AREA);
        CacheUtils.removeAll(CacheUtils.SYS_MENU);
        return getSuccessMessageObject("", "");
    }

    /**
     * 是否更新 0 不更新 1 强制更新 2 不强制更新 userType 0是用户，1是经纪人是吗？
     */
    @ResponseBody
    @RequestMapping(value = {"isUpdate"})
    public Object isUpdate(String version, String userType) {

        Map map = new HashMap();
        List list = DictUtils.getDictListMap("HOUSE_JJRBBH");
        if (DataContants.USER_JJR.equals(userType)) {
            list = DictUtils.getDictListMap("HOUSE_YHBBH");
        }

        if (list.size() == 0) {
            throw new BaseException("版本号错误");
        }
        Map mapBbh = (Map) list.get(0);
        String value = mapBbh.get("value").toString();
        if (StringUtils.isEmpty(version)) {
            throw new BaseException("版本号错误");
        }

        String[] s = value.split("_");
        if (s.length != 2) {
            throw new BaseException("版本号错误");
        }
        map.put("versionName", s[0].toString());
        map.put("flag", s[1].toString());
        map.put("url", mapBbh.get("description"));
        map.put("remark", mapBbh.get("remark"));

        int m = CommonUtil.compare(s[0].toString(), version);

        if (m == 0) {
            map.put("flag", "0");
        }
        return getSuccessMessageObject("", map);
    }

    @ResponseBody
    @RequestMapping(value = {"initData"})
    public Object initData() {

        OrgInit orgInit = new OrgInit();
        orgInit.init();
        List list = orgInit.getOrgList();
        for (int i = 1; i < list.size(); i++) {
            Map map = (Map) list.get(i);
            BaiduMap baiduMap = new BaiduMap();
            baiduMap.setCity(map.get("name").toString().trim());
            baiduMap.setCityCd(map.get("id").toString().trim());
            baiduMap.initData();
            if (i == 8)
                break;
        }

        return getSuccessMessageObject("", "");
    }

    @ResponseBody
    @RequestMapping(value = {"initFile"})
    public Object initFile() {

        BaiduMap baiduMap = new BaiduMap();
        baiduMap.run();
        // String url = "http://7xqa78.com1.z0.glb.clouddn.com/0001.ppt";
        // OfficeEntity e = new OfficeEntity();
        // e.setPdfUrl(url);
        // String endUrl = e.savePdfUrl("0002");
        return getSuccessMessageObject("", "");
    }

    @ResponseBody
    @RequestMapping(value = {"sendSms/{appType}"})
    public Object sendSms(@PathVariable("appType") String appType) {

        ChatInstance.getChat(appType).transmissionMessageFromOneToOne(PeoPleType.CLASSROME.getValue(), "18359260221org ", "AAAAA",
                                                                      MessageType.TXT.getValue(), MessageWTType.CLASS_ROOM.getValue());
        return getSuccessMessageObject("", "");
    }
}